package com.bestcem.xm.ticket.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.qdes.grpc.v1.services.BpProjectTicketServiceGrpc;
import com.bestcem.xm.qdes.grpc.v1.services.ListProjectIdMapsRequest;
import com.bestcem.xm.qdes.grpc.v1.services.ListProjectIdMapsResponse;
import com.bestcem.xm.qdes.grpc.v1.services.ProjectIdMap;
import com.bestcem.xm.ticket.grpc.client.dto.user.IdMapNodeDTO;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import zhongyan.xm.base.v1.Base;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 倍市得 qdes 模块相关rpc接口客户端
 *
 * @author ympan <yuming.pan@idiaoyan.com>
 * @version v1.0
 * @date 2021/8/31 14:33
 */
@Service
@Slf4j
public class TicketQdesGrpcService {

    @GrpcClient(GrpcClientNames.DS_QDES)
    private BpProjectTicketServiceGrpc.BpProjectTicketServiceBlockingStub bpProjectTicketServiceBlockingStub;

    /**
     * 复制旧公司 project
     *
     * @param srcId 旧公司ID
     * @param dstId 新公司ID
     * @return IdMapNodeDTO
     * @author <yuming.pan@idiaoyan.com>
     * @date 14:38 2021/8/31
     */
    @Nullable
    public List<IdMapNodeDTO> getProjectMapBySrcIdAndDstId(String srcId, String dstId) {
        ListProjectIdMapsRequest.Builder builder = ListProjectIdMapsRequest.newBuilder();
        builder.setSrcOrgId(srcId);
        builder.setDstOrgId(dstId);
        try {
            ListProjectIdMapsResponse response =
                    bpProjectTicketServiceBlockingStub.listProjectIdMaps(builder.build());
            if (Objects.isNull(response) || Base.Code.ERROR.equals(response.getStatus().getCode())) {
                log.error("[Ticket] QDES getProjectMapBySrcIdAndDstId {}-{} Error; ErrorMsg: {}"
                        , srcId, dstId, response);
                return null;
            }
            return infoToIdMapNodeDTOList(response.getIdMapsList());
        } catch (Exception e) {
            log.error("[Ticket] QDES getProjectMapBySrcIdAndDstId {}-{} Error; ErrorMsg: {}"
                    , srcId, dstId, e.getMessage(), e);
            return null;
        }
    }

    /**
     * List<IdMapNode> to List<IdMapNodeDTO>
     *
     * @param idMapNodes idMapNodes
     * @return List<IdMapNodeDTO>
     * @author <yuming.pan@idiaoyan.com>
     * @date 14:43 2021/8/31
     */
    @Nonnull
    private static List<IdMapNodeDTO> infoToIdMapNodeDTOList(List<ProjectIdMap> idMapNodes) {
        List<IdMapNodeDTO> idMapNodeDTOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(idMapNodes)) {
            for (ProjectIdMap idMapNode : idMapNodes) {
                IdMapNodeDTO idMapNodeDTO = infoToIdMapNodeDTO(idMapNode);
                if (Objects.nonNull(idMapNodeDTO)) {
                    idMapNodeDTOList.add(idMapNodeDTO);
                }
            }
        }
        return idMapNodeDTOList;
    }

    /**
     * idMapNode to IdMapNodeDTO
     *
     * @param idMapNode idMapNode
     * @return IdMapNodeDTO
     * @author <yuming.pan@idiaoyan.com>
     * @date 14:44 2021/8/31
     */
    @Nullable
    private static IdMapNodeDTO infoToIdMapNodeDTO(ProjectIdMap idMapNode) {
        if (Objects.isNull(idMapNode)) {
            return null;
        }
        IdMapNodeDTO idMapNodeDTO = new IdMapNodeDTO();
        idMapNodeDTO.setOldId(idMapNode.getOldId());
        idMapNodeDTO.setNewId(idMapNode.getNewId());
        return idMapNodeDTO;
    }
}
    